package com.servlet;

import com.Mapper.UserMapper;
import com.alibaba.fastjson.JSON;
import com.bean.Parked;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/SelectTodayOutServlet")
public class SelectTodayOutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        Date date = new Date();
        SimpleDateFormat formatterA = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat formatterB = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String startTime = formatterA.format(date);
        String endTime = formatterB.format(date);
        PrintWriter out = response.getWriter();

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Parked> parkeds = mapper.selectTodayOut(startTime,endTime);
        String jsonString = JSON.toJSONString(parkeds);
        out.print(jsonString);
    }
}
